<template>
  <view class="IntelligentSeal">
    <view class="image-warp">
      <image class="image" src="../../../static/image/Bluetooth/seal.png"></image>
      <view class="text">{{ data_info.name }}</view>
      <view class="subtext">SN:{{ data_info.uuid }}</view>
    </view>
    <u-cell-group v-if="data_info">
      <u-cell-item :title="$t('申请用章')" @click="navigateTo('申请用章')">
        <u-icon slot="icon" custom-prefix="iconfont" name="iconfont icon-shenqingyongzhang" size="40"></u-icon>
      </u-cell-item>
      <u-cell-item :title="$t('我的申请')" @click="navigateTo('我的申请')">
        <u-icon slot="icon" custom-prefix="iconfont" name="iconfont icon-wodeshenqing" size="40"></u-icon>
      </u-cell-item>
      <u-cell-item v-if="computedkeeper()" :title="$t('更换印章')" @click="navigateTo('更换印章')">
        <u-icon slot="icon" custom-prefix="iconfont" name="iconfont icon-yinzhang" size="40"></u-icon>
      </u-cell-item>
      <u-cell-item :title="$t('设置wifi')" @click="navigateTo('设置wifi')">
        <u-icon slot="icon" name="wifi" size="40"></u-icon>
      </u-cell-item>
      <u-cell-item v-if="computedkeeper()" :title="$t('临时盖章')" @click="navigateTo('临时盖章')">
        <u-icon slot="icon" custom-prefix="iconfont" name="iconfont icon-yinzhang" size="40"></u-icon>
      </u-cell-item>
      <u-cell-item v-if="computedkeeper()" :title="$t('远程盖章')" @click="modal.show=true">
        <u-icon slot="icon" custom-prefix="iconfont" name="iconfont icon-yinzhang" size="40"></u-icon>
      </u-cell-item>
      <u-cell-item v-if="computedkeeper()" :title="$t('印章详情')" @click="navigateTo('印章详情')">
        <u-icon slot="icon" name="file-text" size="40"></u-icon>
      </u-cell-item>
      <u-cell-item :title="$t('设备信息')" @click="navigateTo('设备信息')">
        <u-icon slot="icon" name="lock-open" size="40"></u-icon>
      </u-cell-item>
      <u-cell-item v-if="computedkeeper()" :title="$t('解绑并重置')" @click="navigateTo('解绑并重置')">
        <u-icon slot="icon" name="lock-open" size="40"></u-icon>
      </u-cell-item>
    </u-cell-group>
    <!-- <authComponent ref="authComponent" :title="auth.title" :subTitle="auth.subTitle" @submit="submit"
      @close="close" :key="componentKey"></authComponent> -->

    <u-modal ref="uModal" v-model="modal.show" :title="$t('提示')" async-close show-cancel-button @confirm="remoteSeal">
      <view class="slot-content">
        <u-input
            v-model="modal.value"
            :placeholder="$t('请输入盖章次数')"
            border
            maxlength="3"
            placeholder-style="color: red;"
            type="number"
        />
      </view>
    </u-modal>
  </view>

</template>

<script>
import {mapGetters, mapMutations} from 'vuex'

export default {
  components: {},
  computed: {
    ...mapGetters(["userInfo", 'api', 'Bluetooth']),
    computedkeeper() {
      return () => {
        return this.data_info.keeperId === this.userInfo.userId || this.data_info.ownerId === this.userInfo.userId
      }
    }
  },
  data() {
    return {
      modal: {
        show: false,
        value: ''
      },
      signetId: '',
      data_info: '',
      auth: {
        title: '',
        subTitle: this.$t('请输入认证密码，以验证身份')
      },
      from: '',
      componentKey: 0,
    };
  },
  onLoad(e) {
    uni.setNavigationBarTitle({title: this.$t('智能印章')})

    this.$lizhao.bluetooth.initParams()
    this.signetId = e.id
    this.from = e.from
    //监听回复
    uni.$off('onBLECharacteristicValueChange')
    uni.$on('onBLECharacteristicValueChange', (res) => {
      //console.log(res);
      if (res.cmd === 7) {//设备绑定
      }
    })

  },
  onShow() {
    //重新渲染组件否则会出现指纹弹框不出现
    this.componentKey += 1;

    if (this.from === '扫码') {
      //获取印章详情
      this.findBySn()
    } else {
      //获取印章详情
      this.getBySignet()
    }
  },
  onBackPress(options) {
    uni.closeBluetoothAdapter();
    if (options.from === 'navigateBack') {
      return false;
    }
    if (this.from === '扫码') {
      uni.navigateBack({
        delta: 2
      });
    } else {
      uni.navigateBack({
        delta: 1
      });
    }

    return true;
  },
  methods: {
    unbinding() {
      uni.showModal({
        title: this.$t('解绑并重置'),
        content: this.$t('解绑后，组织成员将无法使用该设备，确认要解绑吗') + '？',
        success: res => {
          if (res.confirm) {
            uni.showLoading({
              title: this.$t('解绑中') + '...',
              mask: false
            });
            this.$u.post(this.api.GLOBAL_device_dingSignet_unbinding, {sn: this.data_info.uuid}).then(res => {
              uni.hideLoading()
              if (res.code === 200) {
                uni.showToast({
                  title: res.msg,
                  icon: "none"
                });
                setTimeout(() => {
                  uni.navigateBack({
                    delta: 2
                  });
                }, 1000)
              }
            })
          }
        },
        fail: () => {
        },
        complete: () => {
        }
      });
    },
    //获取印章详情[扫码]
    findBySn() {
      this.$u.get(this.api.GLOBAL_signet_findBySn, {sn: this.signetId}).then(res => {
        if (res.code === 200) {
          this.data_info = res.data
        }
      })
    },
    //获取当前页面的数据
    getBySignet() {
      this.$u.post(this.api.GLOBAL_signet_getBySignet, {signetId: this.signetId}).then(res => {
        if (res.code === 200) {
          this.data_info = res.data
        }
      })
    },

    //申请印章
    navigateTo(text) {
      let data = `https://pages.dingtalk.com/wow/dingtalk/act/smartdevice?service=442&conn=qr&sign=3679b9&sn=${this.data_info.uuid}`
      if (text === '临时盖章') {
        this.idUse(text, data)
      } else if (text === '我的申请') {
        uni.navigateTo({url: '../../application/applicationList/applicationList?name=我的申请&deviceId=' + this.data_info.id + '&orgId=' + this.data_info.orgId});
      } else if (text === "设置wifi") {
        this.idUse(text, data)
      } else if (text === '更换印章') {
        this.idUse(text, data)
      } else if (text === '申请用章') {
        uni.navigateTo({url: '../../application/applicationSeal/applicationSeal'});
      } else if (text === '印章详情') {
        uni.navigateTo({url: '../../application/sealDetails/sealDetails?id=' + this.data_info.id});
      } else if (text === '设备信息') {
        this.idUse(text, data)
      } else if (text === '解绑并重置') {
        this.idUse(text, data)
      }
    },
    remoteSeal() {
      if (this.modal.value.indexOf('.') === -1) {
        let req = {
          deviceId: this.data_info.id,
          count: this.modal.value
        }
        this.$u.post(this.api.GLOBAL_device_signet_remoteUnlock, req).then(res => {
          this.$refs.uModal.clearLoading();
          if (res.code === 200) {
            uni.showToast({
              title: res.msg
            });
            this.modal.show = false
          }
        })
      } else {
        uni.showToast({
          title: this.$t('请输入正整数'),
          icon: "none"
        });
        this.$refs.uModal.clearLoading();
      }

    },
    idUse(text, data) {
      this.$u.post(this.api.GLOBAL_device_dingSignet_idUse, {sn: this.data_info.uuid}).then(res => {
        if (res.code === 200) {
          if (text === '临时盖章') {
            uni.navigateTo({url: '../../tabBar/bluetooth?data=' + encodeURIComponent(data) + '&from=临时盖章&deviceId=' + this.data_info.id + '&orgId=' + this.data_info.orgId});
          } else if (text === '更换印章') {
            uni.navigateTo({url: '../../tabBar/bluetooth?data=' + encodeURIComponent(data) + '&from=更换印章&deviceId=' + this.data_info.id + '&orgId=' + this.data_info.orgId});
          } else if (text === '设备信息') {
            uni.navigateTo({url: '../../tabBar/bluetooth?data=' + encodeURIComponent(data) + '&from=设备信息&deviceId=' + this.data_info.id + '&orgId=' + this.data_info.orgId});
          } else if (text === '解绑并重置') {
            this.unbinding(text)
          } else if (text === '设置wifi') {
            uni.navigateTo({url: '../../tabBar/bluetooth?data=' + encodeURIComponent(data) + '&from=设置wifi&deviceId=' + this.data_info.id + '&orgId=' + this.data_info.orgId});
          }
        }
      });
    },
  }
}
</script>

<style lang="scss" scoped>
.IntelligentSeal {
  background-color: #f0f0f0;
  width: 100%;
  height: 100%;
  padding: 20rpx 0;
  box-sizing: border-box;

  .image-warp {
    width: 100%;
    height: 350rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20rpx;

    .image {
      width: 300rpx;
      height: 200rpx;
    }

    .text {
      font-size: 30rpx;
      line-height: 48rpx;
      color: #000;
    }

    .subtext {
      color: #888;
      font-size: 24rpx;
      line-height: 60rpx;
    }
  }

  .slot-content {
    padding: 20rpx;
  }
}
</style>
